package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/roleSetting")
public class RoleSettingController {

    @Reference
    private RoleSettingService roleSetingService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return roleSetingService.findPage(queryPageBean);
    }

    //查询所有检查项，展示在检查项信息对应的表格中
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            Map all = roleSetingService.findAll();
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //用于前提提交表单，包括两部分：套餐基本信息、关联的检查组id数组
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissions, Integer[] menus) {
        try {
            roleSetingService.add(role, permissions, menus);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //编辑页面提交
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissions, Integer[] menus){

        try {
            roleSetingService.edit(role,permissions,menus);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //数据回显
    @RequestMapping("/editecho")
    public Result editecho(int id) {
        try {
            Map role = roleSetingService.editecho(id);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            roleSetingService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
